<?php 
class boutique_creer extends MX_Controller{
	public function index(){

		$title="Vous créez votre boutique ";
		$description="Ouvrir une boutique gratuitement sur Caverne";
		$keywords = "Ouvrir une boutique, boutique gratuiment, Ouvrir boutique Caverne";

		$this->load->driver('cache');
		
		// Save into the cache for 5 minutes
		if ( ! $annonce_category = $this->cache->file->get('annonce_category'))
		{
			$category= $this->load->module('category/index');
			$annonce_category=$category->dropdown_option_annonce_cats();
			$this->cache->file->save('annonce_category', $annonce_category, 300);
		}
		
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'annonce_category'=>$annonce_category,
				'boutique'=>'');
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
		
			$this->view(
					array('header',
							'boutique-creer',
							'annonce-deposer-footer'),
							$data); // Display the page
	}
	public function valide(){
		  
		$this->load->helper('url');
		$this->load->helper('text');
		$shop=new stdclass;
		$shop->company_name= $this->input->post('nom_boutique');
		$shop->shop_link= url_title(convert_accented_characters($this->input->post('nom_boutique')));
		$shop->email= $this->input->post('email');//todo
		$shop->tilte= $this->input->post('tilte');
		$shop->subtilte= $this->input->post('subtilte');		
		$shop->description= $this->input->post('description');
		
		$shop->shop_active= 0;
		$shop->shop_tag= $this->input->post('shop_tag');
		$shop->shop_keywords= $this->input->post('shop_keywords');
		$shop->date_create= strtotime('now');
		 
		$shop->siren= $this->input->post('siren');
		$shop->adresse= $this->input->post('adresse');
		$shop->region= $this->input->post('region');
		$shop->departement= $this->input->post('departement');
		$shop->code_postal= $this->input->post('code_postal');
		
		$shop->password= $this->input->post('password');
		$shop->frist_name= ucwords(strtolower($this->input->post('nom')));
		$shop->last_name= strtoupper($this->input->post('prenom'));
		$shop->site= $this->input->post('site');
		$shop->open= $this->input->post('horaies');
		$shop->category_id= $this->input->post('category');
		$shop->telephone= $this->input->post('telephone');
		$shop->youtube= $this->input->post('youtube');
			
		$photos=$this->input->post('photo');
		//deplacer les photo dans bon dossier de categories
		$shop->photo=self::copy_Photo(FCPATH."images",'logos',$photos);
		$getBoutique=$this->load->Module('boutique/show');
		
		if(self::check_doublon($shop)){
			$id=$this->model('shop_models_admin')->create($shop);

			if($id) $shop=$getBoutique->get_info($id,false);
			
			
			
			//send email
			$email= $this->load->Module('send/email');
			//load template email
			$setting=$this->load->Module('setting/admin');
			$from_email=$setting->get_admin('admin_email');
				
			$action='activer_boutique';
			$edition=$this->load->Module('setting/edition');
			$edition_send=$edition->get_Edition($action);
			$from_name='Caverne.fr';
			$to_email=$shop->email;
			$subject="Activez votre annonce sur $from_name : pour ".$shop->company_name;
			$message=$edition_send[0]->setting;
			$message_data=(array)$shop;
			$message_data['from_email']=$from_email;
			foreach ($message_data as $key=>$data){
				if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
			}
			$status='send';
			$description='';
			
			$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
			
			
			
			
			
			$message= "Un email de confirmation vient de vous être envoyé <br/>
Pour valider votre boutique, cliquez sur le lien présent dans l'email. Merci de votre confiance!";
		}else {
			
			$id=$this->model()->get_doublon($shop);
			if($id) $shop=$getBoutique->get_info($id,false);
			$message= "Votre boutique n'est pas valide, ou vous avez créé ce boutique,
			retounez <a href='{$this->config->config['base_url']}boutique/creer'>l'étape précendant</a>";
		}
		
		$title="Vous avez créer votre boutique : ".$shop->company_name;
		$description="boutique ".$shop->company_name;
		$keywords = preg_split("/[\s,]+/", $shop->company_name);
		if(is_array($keywords))$keywords=implode(',',$keywords);
		
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'message'=>$message,
				'boutique'=>$shop);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
			$this->view(
					array('header',
							'boutique-creer-confirmer',
							'boutique-info',
							'footer'),
					$data); // Display the page
	}

	private function check_doublon($shop){
		if(is_object($shop) and
		isset($shop->company_name) and $shop->company_name!='' and
		isset($shop->email) and $shop->email!='' and
		isset($shop->category_id) and is_numeric($shop->category_id) ){
			return $this->model()->check_doublon($shop);
		}
		return false;
	}
	
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo="$path/$photo";
			$temp_thumb_photo="$path/thumbnails/".$photo;
			
			//$NameCatPhoto=$cat."/$photo";
			$NameCatPhoto=$photo;
			if(!is_dir("$path/$cat"))mkdir("$path/$cat", 0777);
			if(!is_dir($path."/thumbnails/".$cat))mkdir($path."/thumbnails/".$cat, 0777);
			
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,"$path/$cat/thumbnails/$photo")) unlink($temp_thumb_photo);

			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
}